package Exam1;

import java.util.*;

public class Test1 {
    public static void main(String[] args) {
        try {
            List<String> list = new ArrayList<>();
            // 初始化球并接收返回的列表
            list = printBall(list);

            List<String> list1 = new ArrayList<>();
            // 初始化中奖列表
            List<String> list2 = new ArrayList<>();
            list2.add("red10");
            list2.add("red12");
            list2.add("red30");
            list2.add("red16");
            list2.add("red7");
            list2.add("red17");
            list2.add("blue12");

            int index = 0;
            // 调整循环条件：抽取7个球（6红1蓝）
            while (index < 7) {
                if (index < 6) {
                    // 抽取红球：只从剩余红球中抽取
                    List<String> remainingReds = new ArrayList<>();
                    for (String ball : list) {
                        if (ball.startsWith("red")) {
                            remainingReds.add(ball);
                        }
                    }

                    // 修复随机数生成逻辑
                    int num = (int) (Math.random() * remainingReds.size());
                    String selected = remainingReds.get(num);
                    list1.add(selected);
                    list.remove(selected); // 从原始列表中移除已选中的球
                } else {
                    // 抽取蓝球：只从剩余蓝球中抽取
                    List<String> remainingBlues = new ArrayList<>();
                    for (String ball : list) {
                        if (ball.startsWith("blue")) {
                            remainingBlues.add(ball);
                        }
                    }


                    Random r = new Random();
                    int num = r.nextInt(remainingBlues.size());
                    String selected = remainingBlues.get(num);
                    list1.add(selected);
                    list.remove(selected);
                }
                index++;
            }

            System.out.println("排序前：");
            for (String s : list1) {
                System.out.print(s + " ");
            }

            // 按数字排序
            Collections.sort(list1, (o1, o2) -> {
                int num1 = Integer.parseInt(o1.replaceAll("\\D", ""));
                int num2 = Integer.parseInt(o2.replaceAll("\\D", ""));
                return Integer.compare(num1, num2);
            });

            System.out.println("\n排序后：");
            for (String s : list1) {
                System.out.print(s + " ");
            }

            countBalls(list1, list2);

        } catch (NullPointerException e) {
            System.err.println("发生空指针异常：" + e.getMessage());
            e.printStackTrace();
        } catch (IndexOutOfBoundsException e) {
            System.err.println("发生索引越界异常：" + e.getMessage());
            e.printStackTrace();
        } catch (NumberFormatException e) {
            System.err.println("数字格式转换错误：" + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            System.err.println("发生未知异常：" + e.getMessage());
            e.printStackTrace();
        }
    }

    public static List<String> printBall(List<String> ball) {
        if (ball == null) {
            throw new IllegalArgumentException("输入的列表不能为null");
        }

        ball.clear(); // 清空列表，避免重复添加
        for (int i = 1; i <= 50; i++) {
            if (i <= 35) {
                ball.add("red" + i);
            } else {
                ball.add("blue" + (i - 35));
            }
        }
        Collections.shuffle(ball);
        return ball;
    }

    public static void countBalls(List<String> list1, List<String> list2) {
        if (list1 == null || list2 == null) {
            throw new IllegalArgumentException("传入的列表不能为null");
        }

        int redCount = 0, blueCount = 0;
        for (String s : list1) {
            if (list2.contains(s)) { // 修复判断逻辑，使用contains方法
                if (s.startsWith("red")) {
                    redCount++;
                } else if (s.startsWith("blue")) {
                    blueCount++;
                }
            }
        }
        System.out.println("\n中奖情况：红球" + redCount + "个，蓝球" + blueCount + "个");
    }
}
